package cn.edu.scau.cmi.chenyuanying.orm.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import cn.edu.scau.cmi.chenyuanying.orm.domain.Student;
import cn.edu.scau.cmi.chenyuanying.orm.domain.Teacher;

public class TeacherDAO extends BaseDAO{
	public void add(Teacher s) {
		Session session = getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(s);
			tx.commit();
		} catch(RuntimeException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();
		}				
	}
	
	public void update(Teacher s) {		
		Session session = getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(s);
			tx.commit();
		} catch(RuntimeException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}
	
	public void delete(int id) {
		Session session = getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Object s = session.get(Teacher.class, id);
			session.delete(s);
			tx.commit();
		} catch(RuntimeException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}
	
	public List<Teacher> findAll() {
		Session session = getSession();
		Transaction tx = null;
		List<Teacher> list = null;
		try {
			String hql = "from Teacher";
			Query<Teacher> query = session.createQuery(hql);
			list = query.list();
		} catch(RuntimeException e) {
			throw e;
		} finally {
			session.close();
		}
		return list;
	}
}
